﻿// -----------------------------------------------------------------------
// <copyright file="BaseControl.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.ControlBuilders
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using Gui.Presentation;
    using Gui.ThemeEngine;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class BaseControl
    {
        /// <summary>
        /// Initializes a new instance of the BaseControl class.
        /// </summary>
        public BaseControl()
        {
        }

        /// <summary>
        /// Apply common properties for objects
        /// </summary>
        /// <param name="control">The Xml node that contains our properties</param>
        /// <param name="customControl">The control that we must create</param>
        public virtual void BuildControl(XmlNode control, Control customControl)
        {
            // Apply common properties
            customControl.Top = PropertyProvider.GetValue(control, "top", 0);
            customControl.Left = PropertyProvider.GetValue(control, "left", 0);
            customControl.Width = PropertyProvider.GetValue(control, "width", 30);
            customControl.Height = PropertyProvider.GetValue(control, "height", 15);
            customControl.Name = control.Name;

            if (PropertyProvider.GetValue(control, "backColor").Name != "Transparent")
            {
                customControl.BackColor = PropertyProvider.GetValue(control, "backColor");
            }

            int tabIndex = PropertyProvider.GetValue(control, "tabIndex", -1);
            if (tabIndex != -1)
            {
                customControl.TabIndex = tabIndex;
            }

            customControl.Enabled = PropertyProvider.GetValue(control, "enabled", true);

            // Apply anchor property
            string anchor = PropertyProvider.GetValue(control, "anchor", "none");
            if (anchor == "left;right")
            {
                customControl.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right);
            }
            else if (anchor == "left")
            {
                customControl.Anchor = AnchorStyles.Left;
            }
            else if (anchor == "right")
            {
                customControl.Anchor = AnchorStyles.Right;
            }
            else if (anchor == "top;right")
            {
                customControl.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
            }
            else if (anchor == "top;left")
            {
                customControl.Anchor = (System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left);
            }

            // Apply docking
            if (PropertyProvider.GetValue(control, "dock", string.Empty) == "Top")
            {
                customControl.Dock = DockStyle.Top;
            }
            else if (PropertyProvider.GetValue(control, "dock", string.Empty) == "Bottom")
            {
                customControl.Dock = DockStyle.Bottom;
            }
            else if (PropertyProvider.GetValue(control, "dock", string.Empty) == "Left")
            {
                customControl.Dock = DockStyle.Left;
            }
            else if (PropertyProvider.GetValue(control, "dock", string.Empty) == "Right")
            {
                customControl.Dock = DockStyle.Right;
            }
            else if (PropertyProvider.GetValue(control, "dock", string.Empty) == "Fill")
            {
                customControl.Dock = DockStyle.Fill;
            }

            // Set background image
            string imageName = PropertyProvider.GetValue(control, "image", string.Empty);
            if (imageName != string.Empty)
            {
                string themeName = Metal.Config.Settings.Instance.GetValue("LastSettings.Theme", "BlackMetal");
                customControl.BackgroundImage = System.Drawing.Image.FromFile(Metal.Config.Paths.ThemesFolder + themeName + "\\" + imageName);
            }

            // Set image layout
            string imageLayout = PropertyProvider.GetValue(control, "imageLayout", string.Empty);
            if (imageLayout != string.Empty)
            {
                if (imageLayout == "None")
                {
                    customControl.BackgroundImageLayout = ImageLayout.None;
                }
                else if (imageLayout == "Center")
                {
                    customControl.BackgroundImageLayout = ImageLayout.Center;
                }
                else if (imageLayout == "Stretch")
                {
                    customControl.BackgroundImageLayout = ImageLayout.Stretch;
                }
                else if (imageLayout == "Tile")
                {
                    customControl.BackgroundImageLayout = ImageLayout.Tile;
                }
                else if (imageLayout == "Zoom")
                {
                    customControl.BackgroundImageLayout = ImageLayout.Zoom;
                }
            }

            // Add context menu (if exists)
            XmlNode contextMenuNode = control.SelectSingleNode("ContextMenu");
            if (contextMenuNode != null)
            {
                ControlBuilder builder = new ControlBuilder();
                customControl.ContextMenuStrip = (ContextMenuStrip)builder.CreateControl(contextMenuNode);
            }

            int fontSize = PropertyProvider.GetValue(control, "fontSize", 0);
            if (fontSize != 0)
            {
                customControl.Font = new Font(customControl.Font.FontFamily, fontSize);
            }
        }
    }
}
